package com.toman.Adpter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.toman.Entity.StateSelect_Entity;
import com.toman.MyApplication;
import com.toman.R;
import com.toman.Util.ToastUtil;

import java.util.ArrayList;

/**
 * Created by  RemoteStart_adpter on Administrator on 2017/11/27 on 10:39.
 */

public class StateSelect_adpter extends BaseAdapter implements View.OnClickListener {
    public interface SignupListClickListener {
        public void signupClick();
    }

    private SignupListClickListener signupListClickListener;
    Handler mHandler;

    //上下文对象
    Context context;
    //
    ArrayList<StateSelect_Entity> arrayList;

    LayoutInflater inflater;
    MyApplication myApplication;

    public StateSelect_adpter(Context context, ArrayList<StateSelect_Entity> arrayList, MyApplication myApplication, Handler handler) {
        this.context = context;
        this.arrayList = arrayList;
        this.myApplication = myApplication;
        this.mHandler = handler;
        inflater = LayoutInflater.from(context);
    }

    public void setSignupListClickListener(SignupListClickListener l) {
        signupListClickListener = l;
    }
    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AddPackage addPackage;

        if (null == convertView) {
            addPackage = new AddPackage();
            convertView = inflater.inflate(R.layout.remotestart_item_select, null, false);
            addPackage.img = (ImageView) convertView.findViewById(R.id.mReImg);
            addPackage.name = (TextView) convertView.findViewById(R.id.mRename);
            addPackage.shut = (TextView) convertView.findViewById(R.id.mReshut);
            addPackage.select = (ImageView) convertView.findViewById(R.id.iv_status);


            convertView.setTag(R.id.type_1, addPackage);
            convertView.setTag(R.id.type_2, position);
            convertView.setOnClickListener(this);

        } else {
            addPackage = (AddPackage) convertView.getTag(R.id.type_1);
        }
        //  addPackage.img.setBackgroundResource(arrayList.get(position).getImg());

        addPackage.name.setText(arrayList.get(position).getName());
        if (arrayList.get(position).getStatus().equals("normal")) {
            addPackage.shut.setText("正常");
            addPackage.img.setBackgroundResource(R.mipmap.img_device_normal);
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote6));
        } else if (arrayList.get(position).getStatus().equals("free")) {
            addPackage.shut.setText("空转");
            addPackage.img.setBackgroundResource(R.mipmap.img_device_free);
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote9));
        } else if (arrayList.get(position).getStatus().equals("fault")) {
            addPackage.shut.setText("待机");
            addPackage.img.setBackgroundResource(R.mipmap.img_device_wait);
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote7));
        } else if (arrayList.get(position).getStatus().equals("Off")) {
            addPackage.shut.setText("关闭");
            addPackage.img.setBackgroundResource(R.mipmap.img_device_off);
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote));
        }
//        if (arrayList.get(position).isSelect()) {
//            addPackage.select.setBackgroundResource(R.mipmap.status_ok);
//        }
//else {
//            addPackage.select.setBackgroundResource(R.mipmap.status_fail);
//
//
//        }

        StateSelect_Entity entity = (StateSelect_Entity) getItem(position);
//icon[position].setBackgroundResource(R.mipmap.rubbish);

        if (entity.isSelect()) {
            addPackage.select.setImageResource(R.mipmap.status_ok1);
            addPackage.select.setVisibility(View.VISIBLE);

        } else {

            addPackage.select.setVisibility(View.GONE);

        }
        return convertView;
    }

    @Override
    public void onClick(View view) {
        Message msg = mHandler.obtainMessage(1);
        mHandler.sendMessage(msg);
        int position = (int) view.getTag(R.id.type_2);
        StateSelect_Entity entity = (StateSelect_Entity) getItem(position);
        ToastUtil.print(entity.getName());
//icon[position].setBackgroundResource(R.mipmap.rubbish);
        AddPackage addPackage = (AddPackage) view.getTag(R.id.type_1);
        if (entity.isSelect()) {
            entity.setSelect(false);
            addPackage.select.setVisibility(View.GONE);
        } else {
            entity.setSelect(true);
            addPackage.select.setVisibility(View.VISIBLE);

            addPackage.select.setImageResource(R.mipmap.status_ok1);

        }
        if (signupListClickListener != null) {
            signupListClickListener.signupClick();
        }

    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {

//这里必须返回true
        return true;
    }

    public class AddPackage {
        ImageView img, select;
        TextView name, shut;
    }


}


